<?php
Namespace Cyfy\Modules\Forms;
use Cyfy\Message;

/**
 * Input class, from which should be all inputs in form derived from.
 *
 * @author Dandelion <dandelion8888@gmail.com>
 * @version 0.1
 */

abstract class Input
{
    /**
     * ID of the input
     * @var string
     */
    public $id = '';
    
    /**
     * Is required
     * @var bool
     */
    public $required = false;
    
    /**
     * Description, show in the bubble.
     * @var string
     */
    public $description = '';
    
    /**
     * Name
     * @var string
     */
    public $name = '';
    
    /**
     * Value
     * @var string
     */
    public $value = '';
    
    /**
     * If is the input in fieldset
     * @var bool
     */
    public $inFieldset = false;
    
    /**
     * Prefix for inputs when posting.
     * @var string
     */
    public $prefix = '';
    
    /**
     * Sets the ID
     * @param string New id.
     */
    function setId($id)
    {
        $this -> id = $id;
    }
    
    /**
     * Loads data from post to the object.
     * @return array array(name => value)
     */
    function loadPostData($prefix)
    {
        // if array, crops it!
        if(strpos($this -> name, "[") && strpos($this -> name, "]")){
            $prevName = $this -> name;
            $code = substr($this -> name, strpos($this -> name, "["), -1);
            $this -> name = substr($this -> name, 0, strpos($this -> name, "["));
            if(!is_array($this -> value))
            {
                if($this -> value != ""){
                    $this -> value = array($this -> value);
                }else{
                    $this -> value = array();
                }
            }
            $this -> value[$code] =  $_POST[$prefix . $prevName];
        }else{
        // not
            $this -> value = $_POST[$prefix . $this -> name];
        }

        // load
        return array($this -> name => $this -> value);
    }
    
    /**
     * Loads the description bubble for the input
     */
    function loadDescription()
    {
        if($this -> description == "")
        {
            return false;
        }
        // load popup
        \Cyfy\Modules\jquery\Plugins :: load("jqueryBubblePopup");
        
        $themePath = \Cyfy\Cyfy ::  getSourcePath('Cyfy/Modules/jqueryBubblePopup/images/jquerybubblepopup-theme');
        // show popup
        \Cyfy\Cyfy ::  javaScript("$(document).ready(function(){                $('.form-input-{$this->name}').CreateBubblePopup({position:'right', innerHtml: '" . addslashes($this -> description) ."' , themeName: 	'all-black', themePath: 	'$themePath'});                $('.form-input-{$this->name}').focus(function(){                    $(this).ShowBubblePopup();                })                });", "inline", "header");
    
    
    }
}